public class MenuAreaTabLayout extends FlowLayout
FlowLayout
to create a specific version of this layout
for the menuArea
tabs.
This class implements a layout that acts as the FlowLayout
layout until there is enough space in the container to position all the components
inside one single row.
When there is not enough space for positioning all the components in one single line
inside the container, differently from the FlowLayout
layout that starts putting
the remaining components on a new line and continues on this way until all of them are
positioned, the MenuAreaTabLayout
layout positions all the components that the
container can contain in one single line and then add, as last component of the line, a special
component with a pop-up linked to it that contains all the remaining components that the
container can't contain in one single line.
In our specific case, the components inside the container that we want to manage are JButton
objects that need to be repositioned when it is not possible anymore to display them all in the same
line after a resizing.
When a resize of the container occurs, this layout checks if there is the need to reposition the components, calculating how many buttons now can be displayed in one single line. If not all the buttons can fit in one line, it puts as many buttons as it can inside the container and the remaining in a pop-up menu linked to a last special button added at the end of the line that shows the remaining ones as a list. *
FlowLayout
,
Serialized FormModifier and Type | Field and Description |
---|---|
private int |
howManyComponentsNow
Registers how many components are in the panel after the last modification,
so we can check if, after a resize, the space is still enough for this number
of components or not and modify the panel depending on this.
|
private MenuAreaButton |
more
The "More" button to add to the container when not all of its
Component
elements fit in one single row. |
private JPopupMenu |
morePopup
Pop-up used when the user clicks on the More button.
|
private JPanel |
morePopupPanel
The
JPanel panel inside morePopup that contains the MenuAreaButton
buttons that didn't fit in the first row of targetPanel . |
private static long |
serialVersionUID
Auto-Generated Serial Version UID for this class.
|
private JPanel |
targetPanel
The
JPanel component that this layout has to manage
(we need it here for setting the layout for this specific container). |
private Component[] |
targetPanelComponents
This array contains the
Component elements of the container
that have been added to it at the start of the application. |
Constructor and Description |
---|
MenuAreaTabLayout(int align,
int hgap,
int vgap,
JPanel target)
Constructor with alignment, horizontal gap, vertical gap and the target
JPanel container as parameters that calls the super class
constructor to create a layout with the specified information associated
with the target container. |
MenuAreaTabLayout(int align,
JPanel target)
Constructor with alignment and the target
JPanel container as
parameters that calls the MenuAreaTabLayout(int, int, int, JPanel)
constructor to create a layout with the specified alignment and default hgap and vgap
(hgap=0, vgap=0) associated with the target container. |
MenuAreaTabLayout(JPanel target)
Constructor with the target
JPanel container as parameter that
calls the MenuAreaTabLayout(int, int, int, JPanel) constructor to
create a layout with default parameters (align=LEFT, hgap=0, vgap=0) associated
with the target container. |
Modifier and Type | Method and Description |
---|---|
private int |
howManyInOneRow()
Returns the number of components that fits in one single row
in the
target container in the specific moment
when the method is called (the container can have different
sizes in different moments). |
void |
layoutContainer(Container target)
Lays out the container.
|
private MenuAreaButton |
newMenuAreaButton(String buttonText,
String buttonIconURL,
String buttonToolTip,
String buttonActionCommand)
Creates a new
MenuAreaButton button with text,
icon (optional), tool tip and action command. |
private void |
setMoreButton()
Sets the graphic details of the pop-up menu panel and add it to the pop-up menu
of the More button.
|
addLayoutComponent, getAlignment, getAlignOnBaseline, getHgap, getVgap, minimumLayoutSize, preferredLayoutSize, removeLayoutComponent, setAlignment, setAlignOnBaseline, setHgap, setVgap, toString
private static final long serialVersionUID
private JPanel targetPanel
JPanel
component that this layout has to manage
(we need it here for setting the layout for this specific container).JPanel
private Component[] targetPanelComponents
Component
elements of the container
that have been added to it at the start of the application.Component
private MenuAreaButton more
Component
elements fit in one single row.Component
private JPopupMenu morePopup
JPopupMenu
private JPanel morePopupPanel
JPanel
panel inside morePopup
that contains the MenuAreaButton
buttons that didn't fit in the first row of targetPanel
.JPanel
,
MenuAreaButton
private int howManyComponentsNow
public MenuAreaTabLayout(JPanel target)
JPanel
container as parameter that
calls the MenuAreaTabLayout(int, int, int, JPanel)
constructor to
create a layout with default parameters (align=LEFT, hgap=0, vgap=0) associated
with the target container.target
- the target container to manageJPanel
public MenuAreaTabLayout(int align, JPanel target)
JPanel
container as
parameters that calls the MenuAreaTabLayout(int, int, int, JPanel)
constructor to create a layout with the specified alignment and default hgap and vgap
(hgap=0, vgap=0) associated with the target container.align
- alignment of the positioned componentstarget
- the target container to manageJPanel
public MenuAreaTabLayout(int align, int hgap, int vgap, JPanel target)
JPanel
container as parameters that calls the super class
constructor to create a layout with the specified information associated
with the target container.align
- alignment of the positioned componentshgap
- horizontal gap between componentsvgap
- vertical gap between componentstarget
- the target container to manageJPanel
private MenuAreaButton newMenuAreaButton(String buttonText, String buttonIconURL, String buttonToolTip, String buttonActionCommand)
MenuAreaButton
button with text,
icon (optional), tool tip and action command.buttonText
- the text of the buttonbuttonIconURL
- the URL of the image to display on the buttonbuttonToolTip
- the tool tip to displaybuttonActionCommand
- the action command of the buttonMenuAreaButton
button.private void setMoreButton()
public void layoutContainer(Container target)
When there is not enough space for positioning all the components with their preferred size in one single row inside the container, the method first finds the number of components that it can display in one row and it positions them on the container, then it adds a More button at the end of the line with a pop-up menu linked to it, containing the remaining components that didn't fit in the first row of the container.
layoutContainer
in interface LayoutManager
layoutContainer
in class FlowLayout
target
- the specified container being laid outprivate int howManyInOneRow()
target
container in the specific moment
when the method is called (the container can have different
sizes in different moments).